package com.haulmont.asu.web.ui.gantt;

import com.haulmont.asu.entity.Step;
import com.haulmont.asu.entity.Work;

import java.util.Date;

/**
 * Author: Sergey42
 * Date: 22.12.13 19:53
 */
public class StepAdapter implements Item {

    Step step;

    StepAdapter(Step step) {
        this.step = step;
    }

    @Override
    public String getName() {
        return step.getName();
    }

    @Override
    public Date factStart() {
        Date minDate = null;
        for (Work work : step.getWorks()) {
            if (work.getFactStartDate() != null) {
                if (minDate == null) {
                    minDate = work.getFactStartDate();
                } else if (work.getFactStartDate().before(minDate)) {
                    minDate = work.getFactStartDate();
                }
            }
        }
        return minDate;
    }

    @Override
    public Date factFinish() {
        Date maxDate = null;
        for (Work work : step.getWorks()) {
            if (work.getFactFinishDate() != null) {
                if (maxDate == null) {
                    maxDate = work.getFactFinishDate();
                } else if (work.getFactFinishDate().after(maxDate)) {
                    maxDate = work.getFactFinishDate();
                }
            }
        }
        return maxDate;
    }

    @Override
    public Date planStart() {
        Date minDate = null;
        for (Work work : step.getWorks()) {
            if (work.getPlanStartDate() != null) {
                if (minDate == null) {
                    minDate = work.getPlanStartDate();
                } else if (work.getPlanStartDate().before(minDate)) {
                    minDate = work.getPlanStartDate();
                }
            }
        }
        return minDate;
    }

    @Override
    public Date planFinish() {
        Date maxDate = null;
        for (Work work : step.getWorks()) {
            if (work.getPlanFinishDate() != null) {
                if (maxDate == null) {
                    maxDate = work.getPlanFinishDate();
                } else if (work.getPlanFinishDate().after(maxDate)) {
                    maxDate = work.getPlanFinishDate();
                }
            }
        }
        return maxDate;
    }
}
